package com.cmes.system.core.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cmes.jdbc.service.GenericService;
import com.cmes.system.core.entity.RolePermissionEntity;
import com.cmes.system.core.entity.RolePermissionEntity;
import com.cmes.system.core.repository.RolePermissionRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 角色权限关系管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
public class RolePermissionService extends GenericService<RolePermissionRepository, RolePermissionEntity> {

    public boolean deleteByPermissionId(Long permissionId) {
        return delete(Wrappers.<RolePermissionEntity>lambdaUpdate().eq(RolePermissionEntity::getPermissionId, permissionId));
    }

    public boolean deleteByPermissionIds(Collection<Long> permissionIds) {
        return delete(Wrappers.<RolePermissionEntity>lambdaUpdate().in(RolePermissionEntity::getPermissionId, permissionIds));
    }

    public boolean deleteByRoleId(Long roleId) {
        return delete(Wrappers.<RolePermissionEntity>lambdaUpdate().eq(RolePermissionEntity::getRoleId, roleId));
    }

    public boolean deleteByRoleIds(Collection<Long> roleIds) {
        return delete(Wrappers.<RolePermissionEntity>lambdaUpdate().in(RolePermissionEntity::getRoleId, roleIds));
    }

}
